﻿package rd.hardcore 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.URLRequest;
	import fr.digitas.flowearth.conf.Conf;
	import rd.hardcore.engine.datas.StreamBoardManager;
	import rd.hardcore.engine.save.InitUserConf;
	import rd.hardcore.engine.sound.HardcoreGhettoSoundSystem;
	import rd.hardcore.engine.update.ApplicationUpdater;
	import rd.hardcore.events.SoundModifierEvent;
	import rd.hardcore.events.SoundSystemEvent;
	import rd.hardcore.params.GlobalParams;
	import rd.hardcore.ui.EqVisualization;
	import rd.hardcore.ui.pad.PadsManager;
	import rd.hardcore.ui.pad.PadsManagerContainer;
	import rd.hardcore.ui.StopButton;


	
	/**
	 * Flash entry
	 * @version 2.0
	 * @author Thibault Perret & Romain Prache
	 */
	public class Main extends Sprite
	{
		/* VAR */
		private var managersContainer	: PadsManagerContainer;		
		private var soundSystem			: HardcoreGhettoSoundSystem;
		private var spSpect				: EqVisualization;
		private var btStop				: StopButton;

		
		/* Main */
		public function Main() 
		{
			alpha = 0;
			
			var conf : String = "xml/config.xml";
			
			Conf.addEventListener(Event.COMPLETE, configLoaded);
			Conf.loadXml( new URLRequest ( conf ));
		}
				
		/* Config Loaded */
		private function configLoaded(e:Event):void 
		{
			Conf.removeEventListener(Event.COMPLETE, configLoaded);
			
			var initUserConf:InitUserConf = new InitUserConf();
			initUserConf.addEventListener(Event.COMPLETE, display);
			initUserConf.start();
			
			StreamBoardManager.start();
			StreamBoardManager.instance.checkForBoard();
			
			//return;
			
			// params
			GlobalParams.PAD_SIZE 	= Conf.getNumber("sizePad");
			GlobalParams.KEYBOARDS	= Conf.getDatas("pads").children().length();
		}
		
		/* Display */
		private function display(e:Event):void
		{
			//if (!CONFIG::debug) {
				var appUpdate:ApplicationUpdater = new ApplicationUpdater();
				appUpdate.Maj(Conf.getString("updaterXML"));
			//}
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		
		/* Init */
		private function init(e:Event = null):void 
		{
			trace("Main :: Init");
			
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			managersContainer = new PadsManagerContainer();
			addChild(managersContainer);
			
			soundSystem = new HardcoreGhettoSoundSystem();
			
			spSpect = new EqVisualization();
			addChildAt(spSpect, 0);
			
			
			btStop = new StopButton();
			addChild(btStop);
			
			soundSystem.proxyBtStop = btStop;
			
			spSpect.mouseEnabled = false;
			spSpect.mouseChildren = false;
			
			addEventListener(SoundSystemEvent.PLAY_SOUND, playSound);
			addEventListener(SoundSystemEvent.KILL_SOUND, stopSound);
			
			soundSystem.addEventListener(SoundSystemEvent.STOP_SOUND, spSpect.stopPixelize);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyEvent);			
		}
		
		
		
		public function changeSoundSpeed ( e : SoundModifierEvent ):void 
		{
			soundSystem.playbackSpeed = e.newValue;
		}
	
		/* KeyBoard */
		private function handleKeyEvent(e:KeyboardEvent):void 
		{
			managersContainer.handleKeyDown(e);
		}
		
		
		/**
		 * Kill un son par la spaceBar
		 */
		private function stopSound(e:SoundSystemEvent):void 
		{
			//trace(e.target);
			
			// KEYBOARD
			if( e.target is PadsManager)
				soundSystem.closeSound(e);
			// BUTTON	
			else
				soundSystem.closeSound(null);
		}
		
		private function playSound(e:SoundSystemEvent):void 
		{
			soundSystem.playSound(e);
			spSpect.pixelize();
		}
		
	}

}